<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/2
 * Time: 21:11
 */
use Org\Util\Rbac;

function getSysMenu(){
    $menu = array();
    if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])){
        //已经缓存，则从缓存中读取
        $menu = $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
    }else{
        //读取数据库模块列表生成菜单项
        $node = M("Node");
        $id = $node->getField("id");
        $where['level'] = 2;
        $where['status'] = 1;
        $where['pid'] = $id;        //取数据库第一条记录
        $list = $node->where($where)->field("id,name,title")->order('sort asc')->select();
        if(session('_ACCESS_LIST')){
            $accessList = session('_ACCESS_LIST');
        }else{
            $accessList = Rbac::getAccessList(session(C("USER_AUTH_KEY")));
        }

        foreach($list as $key => $module){
            if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || session(C('ADMIN_AUTH_KEY'))){
                $module['access'] = 1;
                $menu[$key] = $module;
            }
        }
    }
    return $menu;
}